package jp.sourceforge.akjrcp.akj_betas;

import java.io.File;
import java.io.IOException;

import jp.sourceforge.akjrcp.flvtools.basic.FLVToolsPage;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.program.Program;

public class FilePlayer {
	public static Log log=LogFactory.getLog(FilePlayer.class);
	
	private static String getExtension(File file){
		int last=file.getName().lastIndexOf(".");
		if(last!=-1){
			return file.getName().substring(last+1);
		}else{
			return null;
		}
	}
	
	private static void doPlay(File player,File file){
		
		Runtime runtime=Runtime.getRuntime();
		String args[]=new String[]{player.getAbsolutePath(),file.getAbsolutePath()};
		log.info(args[0]+","+args[1]);
		try {
			Process p=runtime.exec(args);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static boolean play(File file,String ext){
		boolean played=false;
		if(ext!=null){
			
			IPreferenceStore store=Akj_betasPlugin.getDefault().getPreferenceStore();
			String playerPath=store.getString(FLVToolsPage.PLAYER_PATH);
			if(playerPath.length()>0){
				File player=new File(playerPath);
				if(player.exists()){
					doPlay(player,file);
					return true;
				}
			}
			//i don't know reason why.
			//return Program.launch(file.getAbsolutePath());
			
		Program program=Program.findProgram(ext);
		
		if(program!=null){
			log.info("file:"+file.getAbsolutePath());
			
			//program.execute(file.getAbsolutePath());
			played=true;
		}
		}else{
			log.warn("no extension:"+file);
		}
		return played;
	}
	public static boolean play(File file){
		String ext=getExtension(file);
		return play(file,ext);
	}
}
